package crm.salesOrderProcessing.managedbean;



import commonInfrastructure.accountManagement.entity.SalesmanEntity;
import commonInfrastructure.accountManagement.session.SalesmanSessionBean;
import crm.salesOrderProcessing.entity.SalesOrderEntity;
import crm.salesOrderProcessing.session.SalesOrderSessionBean;
import crm.salesOrderProcessing.util.exception.NoPurchaseOrderException;



import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;


/**
 *
 * @author xhx
 */
@ManagedBean
@ViewScoped
public class ViewPendingOrdersManagedBean {

    private SalesOrderEntity selectedPurchaseOrder;
    private Long userId;
    private SalesmanEntity employee;
    @EJB
    SalesOrderSessionBean salesOrderSessionBean;
    @EJB
    SalesmanSessionBean employeeManager;

    /** Creates a new instance of ViewAllSalesOrderManagedBean */
    public ViewPendingOrdersManagedBean() {
        selectedPurchaseOrder = new SalesOrderEntity();

    }

    public List<SalesOrderEntity> getPurchaseOrderList() throws IOException {
        List<SalesOrderEntity> result = new ArrayList();
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        userId = (Long) request.getSession().getAttribute("userId");
        setEmployee(employeeManager.getSalesmanById(userId));
        try {
            result = salesOrderSessionBean.getPendingLargeOrders(getEmployee());
        } catch (NoPurchaseOrderException e) {
            FacesContext.getCurrentInstance().getExternalContext().redirect("error/error_noPurchaseOrder.xhtml");
        }
        return result;
    }

    public void viewPurchaseOrder(SalesOrderEntity salesOrder) throws IOException {
        selectedPurchaseOrder = salesOrder;
 
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("salesOrder", selectedPurchaseOrder);
        FacesContext.getCurrentInstance().getExternalContext().redirect("displaySalesOrder.xhtml");
    }


    public SalesOrderEntity getSelectedPurchaseOrder() {
        return selectedPurchaseOrder;
    }

    public void setSelectedPurchaseOrder(SalesOrderEntity selectedPurchaseOrder) {
        this.selectedPurchaseOrder = selectedPurchaseOrder;
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public SalesmanEntity getEmployee() {
        return employee;
    }

    public void setEmployee(SalesmanEntity employee) {
        this.employee = employee;
    }


}
